/**
 * Created by DoveXL on 2018/12/8.
 */
$(document).ready(function () {

    queryUserLike(null,null,null,null,null,0);
    $("#datepicker").datepicker({
        autoclose: true,
        todayHighlight: true,
        language:"zh-CN"
    });
    $("#startdatepicker").datepicker({
        autoclose: true,
        todayHighlight: true,
        language:"zh-CN"
    });
    $("#enddatepicker").datepicker({
        autoclose: true,
        todayHighlight: true,
        language:"zh-CN"
    });
    $("#ChangeSigndatepicker").datepicker({
        autoclose: true,
        todayHighlight: true,
        language:"zh-CN"
    });


});


function queryUserByCondition(){
    var volunteername = $("#volunteername").val();
    var volunteerunit = $("#volunteerunit").val();
    if(volunteername=="")
    {
        volunteername=null;
    }else
    {
        volunteername = "%"+volunteername+"%"
    }
    if(volunteerunit=="")
    {
        volunteerunit=null;
    }else {
        volunteerunit="%"+volunteerunit+"%"
    }
    queryUserLike(null,volunteername,null,volunteerunit,null,0);
}




function queryUserLike(IDCard,Name,Gender,Unit,Phonenum,Deleted) {
    $.ajax({
        async: true,
        type: "post",
        url: "/VolunteerPlatform/PersonInfo/CertainVolunteerInfo",
        dataType: "json",
        contentType : 'application/json;charset=utf-8',
        data: JSON.stringify({"page":1,"user_flag":Deleted,"pagesize":5,"IDCard":IDCard,"Name":Name,"Gender":Gender,"Unit":Unit,"Phonenum":Phonenum}),
        cache: false,
        success: function(data) {
            var result = eval(data);
            $("#VolunteerSignInfo tr").empty();
            for (var i = 0; i<result.pagelist.length; i++) {
                var row = CreateSignPersonRow(result.pagelist[i]);
                $("#VolunteerSignInfo").append(row);
            };
            var currentPage = result.currentpage; //当前页数
            var pageCount = result.pagecount; //总页数
            var options = {
                bootstrapMajorVersion: 3, //版本
                currentPage: currentPage, //当前页数
                totalPages: pageCount, //总页数
                numberOfPages: 5,
                shouldShowPage:true,//是否显示该按钮
                itemTexts: function (type, page, current) {
                    switch (type) {
                        case "first":
                            return "首页";
                        case "prev":
                            return "上一页";
                        case "next":
                            return "下一页";
                        case "last":
                            return "末页";
                        case "page":
                            return page;
                    }
                },
                onPageClicked: function (event, originalEvent, type, page) {
                    $.ajax({
                        async: true,
                        url: "/VolunteerPlatform/PersonInfo/CertainVolunteerInfo",
                        type: "post",
                        dataType : "json",
                        contentType : 'application/json;charset=utf-8',
                        data: JSON.stringify({page:page,"user_flag":Deleted,"pagesize":5,"IDCard":IDCard,"Name":Name,"Gender":Gender,"Unit":Unit,"Phonenum":Phonenum}),
                        cache: false,
                        success: function (data) {
                            $("#VolunteerSignInfo tr").empty();
                            var result = eval(data);
                            for (var i = 0; i <result.pagelist.length; i++) {
                                var row = CreateSignPersonRow(result.pagelist[i]);
                                $("#VolunteerSignInfo").append(row);
                            };
                        }
                    });
                }
            };
            $('#UserpageLimit').bootstrapPaginator(options);
        }
    });
}


function CreateSignPersonRow(UserInfo) {
    var row = document.createElement('tr');
    var ChooseCell = document.createElement('td');
    var btnLabel = document.createElement('label');
    var btnCheckbox = document.createElement('input');
    btnCheckbox.setAttribute("type","radio");
    btnCheckbox.setAttribute("name","PersonRadio");
    btnCheckbox.setAttribute("onchange","ChangeBtnAttr()");
    btnLabel.appendChild(btnCheckbox);
    ChooseCell.appendChild(btnLabel);
    row.appendChild(ChooseCell);
    var UserNameCell = document.createElement('td');
    UserNameCell.innerHTML = UserInfo.user_name;
    row.appendChild(UserNameCell);
    var UserIDCardCell = document.createElement('td');
    UserIDCardCell.innerHTML = UserInfo.user_IDCard;
    row.appendChild(UserIDCardCell);
    return row;
}


function AddVolunteerSignFunction() {
    var length = $("input[name = 'PersonRadio']:checked").length;
    if(length==0)
    {
        alert("未选中任何一个志愿者!");
        return false;
    }
    var CheckedNode = $("input[name = 'PersonRadio']:checked").parent().parent();
    var Name = $(CheckedNode).next().html().trim();
    var IDCardNumber = $(CheckedNode).next().next().html().trim();
    var Date = $("#datepicker").val().trim();
    var Time = $("input[name = 'TimeCategory']:checked").val().trim();
    if(Name.length==0 || IDCardNumber.length==0 || Date.length ==0 || Time.length ==0)
    {
        alert("存在未填项!");
        return false;
    }
    $.ajax({
        async: true,
        url: "/VolunteerPlatform/VolunteerSign/checkVolunteerSignInfo",
        type: "post",
        contentType : 'application/json;charset=utf-8',
        data: JSON.stringify({"Name":Name,"IDCardNumber":IDCardNumber,"Date":Date}),
        cache: false,
        success: function (data) {
            if(data=="1")
            {
                $.ajax({
                    async: true,
                    url: "/VolunteerPlatform/VolunteerSign/AddVolunteerSignInfo",
                    type: "post",
                    contentType : 'application/json;charset=utf-8',
                    dataType:"json",
                    data: JSON.stringify({"Name":Name,"IDCardNumber":IDCardNumber,"Date":Date,"Time":Time}),
                    cache: false,
                    success: function (data) {
                        var userinfo = eval(data);
                        $("#BtnAddVolunteerSign").attr("disabled","disabled");
                        $("#BtnAddVolunteerSign").addClass("disabled");
                        var row=CreateAlreadySignPersonRow(userinfo);
                        $("#VolunteerSignAlreadyInfo").append(row);
                    }
                });
            }else
            {
                alert("该用户今天已经签过到!");
                return false;
            }
        }
    });
}

function ChangeBtnAttr() {
    $("#BtnAddVolunteerSign").removeAttr("disabled");
    $("#BtnAddVolunteerSign").removeClass("disabled");
}


function CreateAlreadySignPersonRow(UserInfo) {
    var row = document.createElement('tr');
    var UserNameCell = document.createElement('td');
    UserNameCell.innerHTML = UserInfo.Name;
    row.appendChild(UserNameCell);
    var UserIDCardCell = document.createElement('td');
    UserIDCardCell.innerHTML = UserInfo.IDCardNumber;
    row.appendChild(UserIDCardCell);
    var UserStatusCell = document.createElement('td');
    UserStatusCell.innerHTML = UserInfo.Status;
    row.appendChild(UserStatusCell);
    return row;
}


$(function(){
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        // 获取已激活的标签页的名称
        var activeTab = $(e.target).text();
        if(activeTab=="历史签到")
        {
            queryVolunteerSignFunction();
        }
    });
});


function queryVolunteerSignFunction() {

    var length = $("input[name = 'TimeCategory1']:checked").length;
    if(length==0)
    {
        alert("未选中任何一个时段!");
        return false;
    }else
    {
        var Time = $("input[name = 'TimeCategory1']:checked").val().trim();
    }

    var Unit = $("#Historicalvolunteerunit").val().trim();
    var Name = $("#Historicalvolunteername").val().trim();
    var StartDate = $("#startdatepicker").val().trim();
    var EndDate = $("#enddatepicker").val().trim();
    if(Unit.length==0)
    {
        Unit=null;
    }
    if(Name.length==0)
    {
        Name=null;
    }
    if(EndDate.length!=0 && StartDate!=0)
    {
        if(StartDate<=EndDate)
        {

        }else
        {
            alert("结束时间早于开始时间");
            return false;
        }
    }else if(EndDate.length==0&&StartDate.length==0)
    {
        EndDate=null;
        StartDate=null;
    }else
    {
        alert("不允许只填一个日期");
        return false;
    }
    queryVolunteerSign(Unit,Name,StartDate,EndDate,Time);
}


function queryVolunteerSign(Unit,Name,StartDate,EndDate,Time) {
    $.ajax({
        async: true,
        type: "post",
        url: "/VolunteerPlatform/VolunteerSign/CertainVolunteerSignInfo",
        dataType: "json",
        contentType : 'application/json;charset=utf-8',
        data: JSON.stringify({page:'1',"pagesize":5,"Name":Name,"Unit":Unit,"StartDay":StartDate,"EndDay":EndDate,"Time":Time}),
        cache: false,
        success: function(data) {
            $("#HistoricalVolunteerSignInfo tr").empty();
            var result = eval(data);
            for (var i = 0; i < result.pagelist.length; i++) {
                var row = CreateVolunteerSignRow(result.pagelist[i]);
                $("#HistoricalVolunteerSignInfo").append(row);
            }
            ;
            if (result.pagelist.length > 0) {
                var currentPage = result.currentpage; //当前页数
                var pageCount = result.pagecount; //总页数
                var options = {
                    bootstrapMajorVersion: 3, //版本
                    currentPage: currentPage, //当前页数
                    totalPages: pageCount, //总页数
                    numberOfPages: 8,
                    shouldShowPage: true,//是否显示该按钮
                    itemTexts: function (type, page, current) {
                        switch (type) {
                            case "first":
                                return "首页";
                            case "prev":
                                return "上一页";
                            case "next":
                                return "下一页";
                            case "last":
                                return "末页";
                            case "page":
                                return page;
                        }
                    },
                    onPageClicked: function (event, originalEvent, type, page) {
                        $.ajax({
                            async: true,
                            url: "/VolunteerPlatform/VolunteerSign/CertainVolunteerSignInfo",
                            type: "post",
                            dataType: "json",
                            contentType: 'application/json;charset=utf-8',
                            data: JSON.stringify({
                                page: page,
                                "pagesize": 5,
                                "Name": Name,
                                "Unit": Unit,
                                "StartDay": StartDate,
                                "EndDay": EndDate,
                                "Time": Time
                            }),
                            cache: false,
                            success: function (data) {
                                $("#HistoricalVolunteerSignInfo tr").empty();
                                var result = eval(data);
                                for (var i = 0; i < result.pagelist.length; i++) {
                                    var row = CreateVolunteerSignRow(result.pagelist[i]);
                                    $("#HistoricalVolunteerSignInfo").append(row);
                                }
                                ;
                            }
                        });
                    }
                };
                $('#HistoricalUserpageLimit').bootstrapPaginator(options);
            }else
            {
                $('#HistoricalUserExample').css("display","none");
            }
        }
    });
}

function CreateVolunteerSignRow(UserInfo) {
    var row = document.createElement('tr');
    var ChooseCell = document.createElement('td');
    var btnLabel = document.createElement('label');
    var btnCheckbox = document.createElement('input');
    btnCheckbox.setAttribute("type","radio");
    btnCheckbox.setAttribute("name","VolunteerSignRadio");
    btnLabel.appendChild(btnCheckbox);
    ChooseCell.appendChild(btnLabel);
    row.appendChild(ChooseCell);
    var UserNameCell = document.createElement('td');
    UserNameCell.innerHTML = UserInfo.Name;
    row.appendChild(UserNameCell);
    var UserIDCardCell = document.createElement('td');
    UserIDCardCell.innerHTML = UserInfo.IDCardNumber;
    row.appendChild(UserIDCardCell);
    var UserDateCell = document.createElement('td');
    UserDateCell.innerHTML = UserInfo.DateDay;
    row.appendChild(UserDateCell);
    var UserSegmentCell = document.createElement('td');
    UserSegmentCell.innerHTML = UserInfo.Time;
    row.appendChild(UserSegmentCell);
    return row;
}

function ShowChangeVolunteerSignModal() {

    var count = $("input[name = 'VolunteerSignRadio']:checked").length;
    if(count==0)
    {
        alert("未选中任何行");
    }else if(count>1)
    {
        alert("选中超过一行!");
    }else
    {
        var CheckedNode = $("input[name = 'VolunteerSignRadio']:checked").parent().parent();
        var VolunteerIDCardNumber = $(CheckedNode).next().next().html().trim();
        var VolunteerName = $(CheckedNode).next().html().trim();
        var VolunteerSignDate = $(CheckedNode).next().next().next().html().trim();
        var VolunteerSignTime = $(CheckedNode).next().next().next().next().html().trim();
        $("input[name='TimeCategory2']").attr("checked",VolunteerSignTime);
        $("#ChangeSigndatepicker").val(VolunteerSignDate);
        $("#OriginVolunteerName").html(VolunteerName);
        $("#OriginVolunteerIDCardNumber").html(VolunteerIDCardNumber);
        $("#ChangeVolunteerSignFlag").html("");
        $("#BtnChangeVolunteerSign").removeAttr("disabled");
        $("#BtnChangeVolunteerSign").removeClass("disabled");
        $("#ChangeVolunteerSignModal").modal();
    }
}

function ChangeVolunteerSignFunction() {
    var CheckedNode = $("input[name = 'VolunteerSignRadio']:checked").parent().parent();
    var VolunteerSignDate = $(CheckedNode).next().next().next().html().trim();
    var VolunteerSignTime = $(CheckedNode).next().next().next().next().html().trim();
    var VolunteerIDCardNumber =$("#OriginVolunteerIDCardNumber").html();
    var VolunteerName = $("#OriginVolunteerName").html().trim();
    var Date = $("#ChangeSigndatepicker").val().trim();
    var Time = $("input[name = 'TimeCategory2']:checked").val().trim();
    if(VolunteerSignDate!=Date || VolunteerSignTime!=Time)
    {
        $.ajax({
                async: true,
                url: "/VolunteerPlatform/VolunteerSign/checkVolunteerSignInfo",
                type: "post",
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({
                    "Name": VolunteerName,
                    "IDCardNumber": VolunteerIDCardNumber,
                    "Date": Date
                }),
                success: function (data) {
                    if (data == 1) {
                        $.ajax({
                            async: true,
                            url: "/VolunteerPlatform/VolunteerSign/ChangeVolunteerSignInfo",
                            type: "post",
                            contentType: 'application/json;charset=utf-8',
                            data: JSON.stringify({
                                "Name": VolunteerName,
                                "IDCardNumber": VolunteerIDCardNumber,
                                "SignDate": Date,
                                "SignTime": Time,
                                "OriginDate":VolunteerSignDate
                            }),
                            success: function (data) {
                                if(data=="true")
                                {
                                    $("#ChangeVolunteerSignFlag").html("修改签到信息成功!");
                                    $("#BtnChangeVolunteerSign").attr("disabled","disabled");
                                    $("#BtnChangeVolunteerSign").addClass("disabled","disabled");
                                    queryVolunteerSignFunction()
                                }else
                                {
                                    $("#ChangeVolunteerSignFlag").html("修改签到信息失败!");
                                    return false;
                                }

                            }
                        })
                    } else {
                        alert("该用户在" + Date + "日已经签过到，如有错误请修改时段或删除")
                        return false;
                    }
                }
                })
    }else {
        alert("未修改任何信息!不能提交");
        return false;
    }
}



function ShowDeleteVolunteerSignModal() {

    var count = $("input[name = 'VolunteerSignRadio']:checked").length;
    if(count==0)
    {
        alert("未选中任何行");
    }else if(count>1)
    {
        alert("选中超过一行!");
    }else
    {
        var CheckedNode = $("input[name = 'VolunteerSignRadio']:checked").parent().parent();
        var VolunteerIDCardNumber = $(CheckedNode).next().next().html().trim();
        var VolunteerName = $(CheckedNode).next().html().trim();
        var VolunteerSignDate = $(CheckedNode).next().next().next().html().trim();
        var VolunteerSignTime = $(CheckedNode).next().next().next().next().html().trim();
        $("#DeleteSignDate").html(VolunteerSignDate);
        $("#DeleteSignSegment").html(VolunteerSignTime);
        $("#DeleteVolunteerName").html(VolunteerName);
        $("#DeleteVolunteerIDCardNumber").html(VolunteerIDCardNumber);
        $("#DeleteVolunteerSignFlag").html("");
        $("#BtnDeleteVolunteerSign").removeAttr("disabled");
        $("#BtnDeleteVolunteerSign").removeClass("disabled");
        $("#DeleteVolunteerSignModal").modal();
    }
}



function DeleteVolunteerSignFunction() {
    var CheckedNode = $("input[name = 'VolunteerSignRadio']:checked").parent().parent();
    var VolunteerSignDate = $(CheckedNode).next().next().next().html().trim();
    var VolunteerSignTime = $(CheckedNode).next().next().next().next().html().trim();
    var VolunteerIDCardNumber = $("#DeleteVolunteerIDCardNumber").html();
    var VolunteerName = $("#DeleteVolunteerName").html().trim();
    $.ajax(
        {
            async: true,
            url: "/VolunteerPlatform/VolunteerSign/DeleteVolunteerSignInfo",
            type: "post",
            contentType: 'application/json;charset=utf-8',
            data: JSON.stringify({
                "Name": VolunteerName,
                "IDCardNumber": VolunteerIDCardNumber,
                "SignDate": VolunteerSignDate,
                "SignTime": VolunteerSignTime,
            }),
            success: function (data) {
                if(data=="true")
                {
                    $("#DeleteVolunteerSignFlag").html("删除签到信息成功!");
                    $("#BtnDeleteVolunteerSign").attr("disabled","disabled");
                    $("#BtnDeleteVolunteerSign").addClass("disabled","disabled");
                    queryVolunteerSignFunction()
                }else
                {
                    $("#DeleteVolunteerSignFlag").html("删除签到信息失败!");
                    return false;
                }

            }
        }
    )
}







